/*
 * Created on 28/11/2004
 *
 */
package br.unb.graphics;

import java.awt.BorderLayout;
import java.util.ResourceBundle;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import br.unb.graphics.inference.InferenceFrame;
import br.unb.graphics.qos.QosPanel;
import br.unb.utils.Console;
import br.unb.utils.Resource;

public class GraphicInterface extends JFrame {

    //GUI
    static InterfaceFactory ifactory;
    private static ResourceBundle resources = Resource.GraphicInterface;
    private ControlComponent controlComponent;
    public static TrafficTable trafficTable;
    private NewTrafficPanel newTrafficPanel;
    private GenerateTrafficPanel generateTrafficPanel;
    private static CommunicPanel comPanel;
    private static SynchronismPanel syncPanel;
    private JPanel QosPanel;
    private JDesktopPane inferenceFrame = null;
    public JTabbedPane tabs; //public visibility temporary
    private JTabbedPane statusTabs;
    private LoadingBar bar = new LoadingBar();
    int layer = 0;
    //TODO Constants, (move to another file)
    static String CONSOLE = "console";
    private static String NEW_TRAFFIC = "newTraffic";
    private static String TRAFFIC = "traffic";
    private static String CHAT = "chat";
    private static String SYNC = "synchronize";
    private static String INFERENCE = "inference";
    private static String QOS = "qos";
    public static String HELP = "help";
    //private final Start Start;
    //private final Starting Starting;
    private static GraphicInterface INSTANCE = new GraphicInterface();
    //Default Console
    private StatusPanel messagesConsole = new StatusPanel();
    //Sockets Consoles
    private StatusPanel ControlSocketConsole = new StatusPanel();
    private StatusPanel DispatcherSocketConsole = new StatusPanel();
    private StatusPanel StatusSocketConsole = new StatusPanel();
    //Threads consoles
    private StatusPanel ManagerThreadConsole = new StatusPanel();
    private StatusPanel DispacherThreadConsole = new StatusPanel();
    private StatusPanel StatusThreadConsole = new StatusPanel();

    public static GraphicInterface getInstance() {
        return INSTANCE;
    }

    private GraphicInterface() {
        super(resources.getString("Title"));
        JFrame.setDefaultLookAndFeelDecorated(true);
        ifactory = new InterfaceFactory(resources);
        //Toolbar
        controlComponent = ControlComponent.getInstance();
        //tabs

        tabs = new JTabbedPane();
        {
            //Start = new Start();
            //Starting = new Starting();
            newTrafficPanel = NewTrafficPanel.getInstance();
            generateTrafficPanel = GenerateTrafficPanel.getInstance();
            trafficTable = new TrafficTable();
            comPanel = new CommunicPanel();
            syncPanel = new SynchronismPanel();
            inferenceFrame = new JDesktopPane();
            QosPanel = new QosPanel();
        }
        resetTabs();
        //tabs.setSelectedComponent(newTrafficPanel);

        //status tabs
        statusTabs = new JTabbedPane(JTabbedPane.TOP);
        {

            /*
            //Default console
            messagesConsole.setDocument(Console.getDocument());
            //Sockets
            DefaultStyledDocument s1 = new DefaultStyledDocument();
            ControlSocketConsole.setDocument(s1);
            ManagerSocket.getInstance().setDocument(s1);

            DefaultStyledDocument s2 = new DefaultStyledDocument();
            DispatcherSocketConsole.setDocument(s2);
            DispatcherSocket.getInstance().setDocument(s2);

            DefaultStyledDocument s3 = new DefaultStyledDocument();
            StatusSocketConsole.setDocument(s3);
            StatusSocket.getInstance().setDocument(s3);

            //Threads
            DefaultStyledDocument s4 = new DefaultStyledDocument();
            ManagerThreadConsole.setDocument(s4);
            Manager.getInstance().setDocument(s4);

            DefaultStyledDocument s5 = new DefaultStyledDocument();
            DispacherThreadConsole.setDocument(s5);
            Dispatcher.getInstance().setDocument(s5);

            DefaultStyledDocument s6 = new DefaultStyledDocument();
            DispatcherSocketConsole.setDocument(s2);
            StatusThreadConsole.setDocument(s6);
            Status.getInstance().setDocument(s6);

            statusTabs.addTab(
            "Messages",
            GraphicInterface.ifactory.getIcon(GraphicInterface.CONSOLE),
            messagesConsole);
            statusTabs.addTab(
            "ControlsSocket",
            GraphicInterface.ifactory.getIcon(GraphicInterface.CONSOLE),
            ControlSocketConsole);
            statusTabs.addTab(
            "DispatcherSocket",
            GraphicInterface.ifactory.getIcon(GraphicInterface.CONSOLE),
            DispatcherSocketConsole);
            statusTabs.addTab(
            "StatusSocket",
            GraphicInterface.ifactory.getIcon(GraphicInterface.CONSOLE),
            StatusSocketConsole);
            statusTabs.addTab(
            "Manager",
            GraphicInterface.ifactory.getIcon(GraphicInterface.CONSOLE),
            ManagerThreadConsole);
            statusTabs.addTab(
            "Dispacher",
            GraphicInterface.ifactory.getIcon(GraphicInterface.CONSOLE),
            DispacherThreadConsole);
            statusTabs.addTab(
            "Status",
            GraphicInterface.ifactory.getIcon(GraphicInterface.CONSOLE),
            StatusThreadConsole);
            statusTabs.setSelectedComponent(messagesConsole);
             */
        }
        //setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        //Main window packing
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(controlComponent, BorderLayout.NORTH);
        this.add(tabs, BorderLayout.CENTER);
        //this.add(statusTabs, BorderLayout.SOUTH);

        this.setSize(800, 650);
        this.setLocation(100, 50);
        this.setResizable(true);
        this.setVisible(true);
        Console.println("Visible!");

    }

    public void resetTabs() {
        tabs.removeAll();
        /*tabs.addTab(
        ifactory.getResourceString(NEW_TRAFFIC),
        ifactory.getIcon(NEW_TRAFFIC),
        newTrafficPanel);
         */
        /*tabs.addTab(
        ifactory.getResourceString(TRAFFIC),
        ifactory.getIcon(TRAFFIC),
        trafficTable.getPanel());
         */
        /*tabs.addTab(
        ifactory.getResourceString(CHAT),
        ifactory.getIcon(CHAT),
        comPanel);*/
        /*tabs.addTab(
        ifactory.getResourceString(SYNC),
        ifactory.getIcon(SYNC),
        syncPanel);*/
        //Experimental
        /*tabs.addTab(
        ifactory.getResourceString(INFERENCE),
        ifactory.getIcon(INFERENCE),
        inferenceFrame);
        tabs.addTab(
        ifactory.getResourceString(QOS),
        ifactory.getIcon(QOS),
        QosPanel);*/
        /*tabs.addTab(
        "start",
        ifactory.getIcon(CONSOLE),
        Start);
        tabs.addTab(
        "starting",
        ifactory.getIcon(CONSOLE),
        Starting);
         */
        //tabs.setSelectedComponent(newTrafficPanel);
    }

    public void showNewTrafficPanel() {
        tabs.addTab(
                ifactory.getResourceString(NEW_TRAFFIC),
                ifactory.getIcon(NEW_TRAFFIC),
                newTrafficPanel);
        tabs.setSelectedComponent(newTrafficPanel);
    }

    public void showGenerateTrafficPanel() {
        tabs.addTab(
                "Generate Traffic",
                ifactory.getIcon(NEW_TRAFFIC),
                generateTrafficPanel);
        tabs.setSelectedComponent(generateTrafficPanel);
    }

    public static CommunicPanel getChatPanel() {
        return comPanel;
    }

    public void loadInferenceTab() {
        tabs.add(ifactory.getResourceString(INFERENCE), this.inferenceFrame);
    }

    public void addInternalFrame(InferenceFrame iframe) {
        iframe.setSize(750, 350);
        iframe.setResizable(true);
        iframe.setClosable(true);
        iframe.setMaximizable(true);
        iframe.setIconifiable(true);
        this.inferenceFrame.add(iframe, 0);
        try {
            iframe.setSelected(true);
        } catch (java.beans.PropertyVetoException e2) {
        }
        iframe.show();
    }

    public void removeInferenceTab() {
        tabs.remove(this.inferenceFrame);
        inferenceFrame.removeAll();
        layer = 0;
    }

    public void showTrafficTable() {
        tabs.setSelectedComponent(trafficTable.getPanel());
    }

    public void showSyncPanel() {
        tabs.setSelectedComponent(syncPanel);
    }

    public SynchronismPanel getSyncPanel() {
        return syncPanel;
    }

    public void showChatPanel() {
        tabs.setSelectedComponent(comPanel);
    }

    public void showQosPanel() {
        tabs.setSelectedComponent(QosPanel);
    }

    public void showInferencePanel() {
        tabs.setSelectedComponent(inferenceFrame);
    }

    public void startLoadingBar() {
        bar.run();
        statusTabs.addTab(
                "StatusBar",
                GraphicInterface.ifactory.getIcon(GraphicInterface.CONSOLE),
                bar);
        statusTabs.setSelectedComponent(bar);
    }

    public void stopLoadingBar() {
        bar.stop();
        statusTabs.remove(bar);
        //statusTabs.setSelectedComponent(messagesConsole);
    }
    /*
    public void showStart() {
    tabs.setSelectedComponent(Start);

    }

    public void showStarting() {
    tabs.setSelectedComponent(Starting);
    }*/
}
